package com.sq.partner.manager.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sq.partner.manager.common.DictCache;
import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.mybatis.model.DictDto;
import com.sq.partner.manager.mybatis.model.FacadeUser;
import com.sq.partner.manager.mybatis.model.NocarProd;
import com.sq.partner.manager.service.DictManagerService;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;



/**
 * 
 * @author 
 */
@Controller
@RequestMapping(value = "/dictManager")
public class DictManagerController {
	private static Logger logger = LogUtil.getLog(DictManagerController.class);
	@Autowired
	private DictManagerService dictManagerService;
	
	@RequestMapping(value = "/toDict")
	public String dataFilteConfig(ModelMap model) throws Exception {
		return "dictManager/dictManager";
	}
	
	
	
	
	
	@RequestMapping(value="/queryDictPageByCriteria")
	@ResponseBody
	public Page<DictDto> queryDictList(DictDto dictDto,Page<DictDto> page,ModelMap map1) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		if( dictDto!=null){
			if(StrUtil.isNotEmpty(dictDto.getCodeName())){
				map.put("codeName", "%"+dictDto.getCodeName()+"%");
			}
			if(StrUtil.isNotEmpty(dictDto.getCodeDesc())){
				map.put("codeDesc", "%"+dictDto.getCodeDesc()+"%");
			}
		}
		map.put("BEGINROW", (page.getCurrent()-1) * 10 );
		map.put("ENDROW", page.getCurrent() * 10);
		page.setMap(map);
		page=dictManagerService.queryDictDtoPageByCriteria(page);
		
		return page;
	}
	
	@RequestMapping(value = "/insertDict")
	@ResponseBody
	public Page<DictDto> insertDict(String codeType, String code, String codeName, String codeDesc,String status,String otherCode,String otherName,String parentCode,String otherCode2,String code2Name) throws Exception{
		logger.debug("进入insertDict - controller");
		Page<DictDto> page = new Page<DictDto>();
		if (StrUtil.isEmpty(codeType)) {
			page.setMessage("险种类别不能为空");
			page.setResult("error");
			return page;
		}

		if (StrUtil.isEmpty(code)) {
			page.setMessage("字典值不能为空");
			page.setResult("error");
			return page;
		}
		
		DictDto dict = new DictDto();
		dict.setCode(code);
		dict.setCodeType(codeType);

		DictDto rsDict  = dictManagerService.queryDictDtoPageByInstance(dict);
		if(rsDict != null){
			page.setMessage("此条字典信息已存在！");
			page.setResult("error");
			return page;
		}
		dict.setCodeDesc(codeDesc);
		dict.setCodeName(codeName);
		dict.setCode2Name(code2Name);
		dict.setOtherCode(otherCode);
		dict.setOtherCode2(otherCode2);
		dict.setOtherName(otherName);
		dict.setOtherName2(code2Name);
		dict.setParentCode(parentCode);
		dict.setStatus(status);
		// 目的得到添加产品编号便与险种配置
		dictManagerService.insertGatewayDict(dict);
		page.setMessage("更新成功");
		DictCache.refreshCache();
		return page;
	}
	
	
	@RequestMapping(value = "/updateDict")
	@ResponseBody
	public  Page<DictDto> updateDict(String codeType, String code, String codeName, String codeDesc,String status,String otherCode,String otherName,String parentCode,String otherCode2,String code2Name,String oldCode , String oldCodeType) throws Exception {
		logger.debug("进入updateDict - controller");
		Page<DictDto> page = new Page<DictDto>();
		if (StrUtil.isEmpty(codeType)) {
			page.setMessage("险种类别不能为空");
			page.setResult("error");
			return page;
		}

		if (StrUtil.isEmpty(code)) {
			page.setMessage("字典值不能为空");
			page.setResult("error");
			return page;
		}
		
		DictDto dict = new DictDto();
		dict.setCode(code);
		dict.setCodeType(codeType);
		if (!codeType.equals("school_config")) {
			//如果code或codetype修改过 ， 判断是否重复
			if(!oldCode.equals(code) || !oldCodeType.equals(codeType)){
				DictDto rsDict  = dictManagerService.queryDictDtoPageByInstance(dict);
				if(rsDict != null){
					page.setMessage("此条字典信息已存在！");
					page.setResult("error");
					return page;
				}
			}
		}
		dict.setCodeName(codeName);
		dict.setCode2Name(code2Name);
		dict.setCodeDesc(codeDesc);
		dict.setOtherCode(otherCode);
		dict.setOtherCode2(otherCode2);
		dict.setOtherName(otherName);
		dict.setOtherName2(code2Name);
		dict.setParentCode(parentCode);
		dict.setStatus(status);
		dict.setOldCode(oldCode);
		dict.setOldCodeType(oldCodeType);
		// 目的得到添加产品编号便与险种配置
		dictManagerService.updateGatewayDict(dict);
		page.setMessage("更新成功");
		DictCache.refreshCache();
		return page;
	} 
	
	@RequestMapping(value="/delDict")
	@ResponseBody
	public  Page<DictDto> delDict(String codeType , String code) throws Exception{
		Page<DictDto> page = new Page<DictDto>();
		DictDto dict = new DictDto();
		dict.setCode(code);
		dict.setCodeType(codeType);
		dictManagerService.deleteGatewayDict(dict);
		page.setMessage("删除成功");
		DictCache.refreshCache();
		return page;
	}
	
	
	
/*	public @ResponseBody Result checkUsercodeBeforeAdd(HttpServletRequest req,FacadeUser user,String usercode) throws Exception {
		Map<String,String> map = new HashMap<String, String>();
		map.put("userCode", usercode);
		FacadeUser ur = (FacadeUser)userService.queryUserCountByMap(map);
		if(ur!=null){
			logger.info("---------------用户名已存在------------------");
			return Result.error("该用户名已存在");
		}else{
			return null;
		}*/

	
}
